package com.sky.task;

import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

@Component
@Slf4j
public class mytask {

    @Autowired
    private OrderMapper orderMapper;
    @Scheduled(cron = "0 * * * * ?")//没分钟执行一次 处理超时订单
    public void executeTask(){
//        log.info("定时任务：{}", LocalDateTime.now());
       List<Orders> list= orderMapper.executeTimeOut(Orders.PENDING_PAYMENT,LocalDateTime.now().plusMinutes(-15));
       if(list!=null&&list.size()>0){
           for (Orders orders : list) {
               orders.setStatus(Orders.CANCELLED);
               orders.setCancelReason("订单超时，自动取消");
               orders.setCancelTime( LocalDateTime.now());
               orderMapper.updateOrder(orders);
           }
       }
    }

    @Scheduled(cron = "0 0 1 * * ?")
    public void executeAllDeliveryOrder(){
        List<Orders> list=  orderMapper.executeTimeOut(Orders.COMPLETED,LocalDateTime.now().plusHours(-1));
        if(list!=null&&list.size()>0){
            for (Orders orders : list) {
                orders.setStatus(Orders.COMPLETED);
                orderMapper.updateOrder(orders);
            }
        }
    }
}
